package com.darren.euc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.darren.euc.dao.FunctionDao;
import com.darren.euc.entity.Function;
import com.darren.euc.entity.PageBean;
import com.darren.euc.service.FunctionService;
import org.springframework.util.CollectionUtils;


@Service
public class FunctionServiceImpl implements FunctionService {
	
	@Autowired
    private FunctionDao functionDao;
	

	@Override
	public List<Function> getAll() {
		return functionDao.getAll();
	}

	@Override
	public List<Function> getFunctionsByUserId(Integer id){		
		return functionDao.getFunctionsByUserId(id);
	}

	@Override
	public PageBean queryForPageBySQL(String sql, Integer pageSize, Integer currentPage, Object... objs) {
		return functionDao.queryForPageBySQL(sql, pageSize, currentPage, objs);
	}

	@Override
	public List<Function> getMemberCenterFunctions() {
		//获取级别
		List<Function> list = functionDao.findFunctionsByFunctionId(70000);
		list = this.forChild(list);
		return list;
		//return functionDao.findMemberFunctions();
	}

	private List<Function> forChild(List<Function> list) {
		List<Function> realList = new ArrayList<>();
		if(!CollectionUtils.isEmpty(list)){
			realList.addAll(list);
			for (Function function: list) {
				List<Function> children = functionDao.findFunctionsByParentFunctionId(function.getFunctionId());
				if(!CollectionUtils.isEmpty(children)){
					children = this.forChild(children);
					//function.setChildren(children);
					realList.addAll(children);
				}
			}
		}
		return realList;
	}
}